# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
from itertools import chain
import testjson
from django.core import serializers
from django.http import HttpResponse
from django.shortcuts import render
import models

# Create your views here.
import utils


def index(request):
    return HttpResponse('hello ')


def index2(request):
    article = models.Articale.objects.get(pk=1)
    return render(request, 'index.html', {'hello': article})


def index4(request):
    articles = models.Articale.objects.all()
    user = models.UserBean.objects.all()
    _data_page_json = {}
    actt = utils.queryset_to_json(articles)
    user1 = utils.queryset_to_json(user)
    _data_page_json['Rows'] = actt
    _data_page_json['Total'] = user1
    jsondata = json.dumps(_data_page_json)
    return HttpResponse(jsondata, content_type="application/json")


def index3(request):
    articles = models.Articale.objects.all()
    user = models.UserBean.objects.all()
    return utils.allmoble(act=articles, userbeans=user, page=str(1))


# return render(request, 'index.html', {'hello': articles})


def edit_page(request):
    return render(request, 'edit_page.html')


def edit_action(request):
    title = request.POST.get('title', 'TITLE')
    content = request.POST.get('content', 'CONTENT')
    models.Articale.objects.create(title=title, content=content)
    articles = models.Articale.objects.all()
    return utils.simplejson(articles)


def getsimpletype(request):
    simpletype = models.SimpleType.objects.all()
    return utils.allmoble(data=simpletype)


# 分页查询
def gethomegoodlist(request):
    page = request.GET.get("page")
    int_page = int(page)
    goodsinfo = models.GoodsInfo.objects.all()[(int_page - 1) * 5:int_page * 5]
    return utils.allmoble(data=goodsinfo)


def getpersoninfo(request):
    id = request.GET.get("id")
    personinfo = models.PersonInfo.objects.get(personId=id)
    return utils.singlejson(personinfo.toJson())


def getnearbyinfo(request):
    id = request.GET.get("id")
    typeinfos = models.TypeInfo.objects.filter(typeId=id)
    return utils.allmoble(info=typeinfos)


def getnearbygoodsinfo(request):
    page = request.GET.get("page")
    int_page = int(page)
    goodsinfo = models.GoodsInfo.objects.all()[(int_page - 1) * 5:int_page * 5]
    return utils.allmoble(data=goodsinfo)
